GOCMD = go
GOBUILD = $(GOCMD) build
GOMOD = $(GOCMD) mod
GOTEST = $(GOCMD) test
BINARY_NAME = miao-butler
CLI = adm
VERSION = $(shell sh auto-ver.sh)

all: serve

init:
	$(GOMOD) init $(module)

install:
	$(GOMOD) tidy

serve:
	$(GOCMD) run .

build:
	CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -o ./build/$(BINARY_NAME) -v ./

generate:
	$(CLI) generate -c adm.ini

test: black-box-test user-acceptance-test

black-box-test: ready-for-data
	$(GOTEST) -v -test.run=TestMainBlackBox
	make clean

user-acceptance-test: ready-for-data
	$(GOTEST) -v -test.run=TestMainUserAcceptance
	make clean

ready-for-data:
	cp admin.db admin_test.db

doc:
	sh api/auto-api-gen.sh api ${VERSION} > api/stub.go
	swag init \
		-d api \
		-g stub.go \
		--parseDependency \
		--markdownFiles markdown
	rm -f api/docs/docs.go api/docs/swagger.yaml
	rm -f api/stub.go

clean:
	rm admin_test.db

.PHONY: all serve build generate test black-box-test user-acceptance-test ready-for-data clean

.PHONY: prepare
prepare:
	go install github.com/swaggo/swag/cmd/swag@v1.8.0
	go install github.com/golangci/golangci-lint/cmd/golangci-lint@v1.43.0

.PHONY: fmt
fmt:
	go fmt ./...

.PHONY: vet
vet:
	go vet ./...

PHONY: lint
lint:
	golangci-lint run --enable golint ./...
